app.controller('AppCtrl',['$scope','$ionicLoading','BaseService','$rootScope','$ionicHistory','appFactory','$ionicModal','$state','$cordovaBarcodeScanner','$cordovaGeolocation',
  function($scope,$ionicLoading,BaseService,$rootScope,$ionicHistory,appFactory,$ionicModal,$state,$cordovaBarcodeScanner,$cordovaGeolocation){



    $ionicModal.fromTemplateUrl('templates/login.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
      if(!appFactory.isLogin()){
        modal.show();
      }
    });

    $rootScope.$on('$stateChangeStart',function(){
        //$ionicHistory.clearHistory();
          if(!appFactory.isLogin()){
              $scope.modal.show();
          }

    })
  //appFactory.reload();
    /*
     var posOptions = {timeout: 10000, enableHighAccuracy: false};
     $cordovaGeolocation
     .getCurrentPosition(posOptions)
     .then(function (position) {
     var lat  = position.coords.latitude;
     var long = position.coords.longitude;
     alert('lat:'+lat);
     alert('long:'+long);
     }, function(err) {
     // error
     alert(JSON.stringify(err));
     });
     //*/


    $scope.loginData = {username:"",password:"",namenull:true,pwdnull:true};

    $scope.barscan = function(){
      $cordovaBarcodeScanner
          .scan()
          .then(function(barcodeData) {
              //alert(JSON.stringify(barcodeData));
              var cancelled = barcodeData.cancelled;
              var result = JSON.parse(barcodeData.text);
              //alert('cancelled:'+cancelled);
              //alert('result:'+result);
              if(!cancelled){
                  var oid = result.OID;
                  var storage = window.localStorage;
                  var uid = storage.getItem("uid");
                  if (oid){
                      var Order = AV.Object.extend("Order");
                      var orderQuery = new AV.Query(Order);
                      orderQuery.equalTo('orderid',oid);
                      orderQuery.first().then(function(res){
                          BaseService.barscanHandle(uid,oid).then(function(res){
                              //alert(res.msg);
                              var r=confirm(res.msg+"\r\n是否继续扫码!");
                              if (r==true) {
                                  $scope.barscan();
                              }
                          }).catch(function(err){
                              //alert(err.msg);
                              var r=confirm(res.msg+"\r\n是否继续扫码!");
                              if (r==true) {
                                  $scope.barscan();
                              }
                          });
                      }).catch(function(err){
                          var r=confirm("没有扫描到正确的订单信息!\r\n是否继续扫码!");
                          if (r==true) {
                              $scope.barscan();
                          }
                      });
                  }else{
                      var r=confirm("没有扫描到正确的订单信息!\r\n是否继续扫码!");
                      if (r==true) {
                          $scope.barscan();
                      }
                  }
              }else{
                  alert('扫描异常或退出扫描');
              }
          }, function(error) {
            // An error occurred
          });
    }

      /**
       * 登录
       */
    $scope.doLogin = function(){
        $ionicLoading.show({
        });
      appFactory.doLogin($scope.loginData.username, $scope.loginData.password).then(function (res) {
          $ionicLoading.hide();
          switch (parseInt(res.query2.code)){
             case 0:
                 $scope.modal.hide();
                 window.location.reload();
                 break;
             case 1:
                 alert("账号或密码错误");
                 break;
             case 2:
                 alert("系统错误");
                 break;

         }
      });
    };
  }])


